package cn.iocoder.yudao.module.lfd.service.points;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.lfd.controller.admin.points.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.points.PointsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.lfd.convert.points.PointsConvert;
import cn.iocoder.yudao.module.lfd.dal.mysql.points.PointsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 积分 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PointsService {

    @Resource
    private PointsMapper pointsMapper;

    
    public Long createPoints(PointsCreateReqVO createReqVO) {
        // 插入
        PointsDO points = PointsConvert.INSTANCE.convert(createReqVO);
        points.setCreateTime(LocalDateTime.now());
        pointsMapper.insert(points);
        // 返回
        return points.getId();
    }

    
    public void updatePoints(PointsUpdateReqVO updateReqVO) {
        // 校验存在
        validatePointsExists(updateReqVO.getId());
        // 更新
        PointsDO updateObj = PointsConvert.INSTANCE.convert(updateReqVO);
        pointsMapper.updateById(updateObj);
    }

    
    public void deletePoints(Long id) {
        // 校验存在
        validatePointsExists(id);
        // 删除
        pointsMapper.deleteById(id);
    }

    private void validatePointsExists(Long id) {
        if (pointsMapper.selectById(id) == null) {
//            throw exception(POINTS_NOT_EXISTS);
        }
    }

    
    public PointsDO getPoints(Long id) {
        return pointsMapper.selectById(id);
    }

    
    public List<PointsDO> getPointsList(Collection<Long> ids) {
        return pointsMapper.selectBatchIds(ids);
    }

    
    public PageResult<PointsDO> getPointsPage(PointsPageReqVO pageReqVO) {
        return pointsMapper.selectPage(pageReqVO);
    }

    
    public List<PointsDO> getPointsList(PointsExportReqVO exportReqVO) {
        return pointsMapper.selectList(exportReqVO);
    }

    public void addRecord(Long appointmentId, Double integral, String openid, String type) {
        PointsDO pointsDO = new PointsDO().
                setAppointmentId(appointmentId)
                .setAmount(integral)
                .setOpenid(openid)
                .setType(type);
        this.pointsMapper.insert(pointsDO);
    }
}
